package cn.fourhundredandeighteen.controller;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.entity.*;
import cn.fourhundredandeighteen.service.CourseService;
import cn.fourhundredandeighteen.service.StudentCourseService;
import cn.fourhundredandeighteen.service.StudentService;
import cn.fourhundredandeighteen.service.TeacherService;
import cn.fourhundredandeighteen.service.impl.CourseServiceImpl;
import cn.fourhundredandeighteen.service.impl.StudentCourseServiceImpl;
import cn.fourhundredandeighteen.service.impl.StudentServiceImpl;
import cn.fourhundredandeighteen.service.impl.TeacherServiceImpl;
import cn.fourhundredandeighteen.utill.AdminCheck;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.utill.ResultGenerator;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:教师操作类
 * @author: liuyongqi
 * @time: 2020/12/17
 */
public class TeacherController {
    public Result<List<Teacher>> getTeacherAll(){
        TeacherService teacherService=new TeacherServiceImpl();
        try {
            List<Teacher> teachers = teacherService.selectTeacherAll();
            if (teachers!=null){
                return ResultGenerator.successResult(teachers);
            }
            return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }
    /**
     * 教师个人信息查询--教师查询
     * @param teacherId
     * @return教师个人信息
     * 功能已添加
     */
    public Result<Teacher> teacherInfo(Long teacherId){
        TeacherService teacherService=new TeacherServiceImpl();
        try {
            Teacher teacherInfo = teacherService.getTeacherInfo(teacherId);
            return ResultGenerator.successResult(teacherInfo);
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 创建老师对象--教师入职
     * @param teacher
     * @return
     */
    public Result CreateTeacherCourse(Teacher teacher){
        TeacherService service=new TeacherServiceImpl();
        int teacher1 = service.createTeacher(teacher);
        if (teacher1>0){
            return ResultGenerator.successResult();
        }else
            return ResultGenerator.errorResult();
    }


    /**教师个人信息删除
     * @param teacherId
     * @return
     */
    public Result delTeacher(Long teacherId){
        TeacherService teacherService=new TeacherServiceImpl();
        try {
            int i = teacherService.deleteTeacher(teacherId);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**更新教师信息
     * @param teacher
     * @return
     */
    public Result upTeacher(Teacher teacher){
        TeacherService teacherService=new TeacherServiceImpl();
        try {
            int i = teacherService.upTeacher(teacher);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    //其他操作
    /**
     * 教师创建的课程查询--教师课程
     * @param teacherId
     * @return 课程信息
     * 功能已添加
     */
    public Result<List<Course>> selectTeacherCreateCourse(Long teacherId){
        TeacherService service=new TeacherServiceImpl();
        try {
            Teacher teacherInfo = service.getTeacherInfo(teacherId);
            if (!AdminCheck.adminCheck()) {
                if (!teacherInfo.getTeacherId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            List<Course> courses = service.selectTeacherCourse(teacherId);
            return ResultGenerator.successResult(courses);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**课程成员查询
     * @param courseId 课程id
     * @return 学生信息
     *
     */
    public Result<List<Student>> getCourseStudents(Long courseId){
        StudentCourseService studentCourseService=new StudentCourseServiceImpl();
        CourseService courseService=new CourseServiceImpl();
        //查询课程所有学生
        try {
            Course course = courseService.getCourse(courseId);
            if (!AdminCheck.adminCheck()) {
                if (!course.getTeacherId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            List<StudentCourse> studentCourses = studentCourseService.selectCourseToStudent(courseId);
            if (studentCourses==null)
                return ResultGenerator.errorResult();
            List<Long> studentIds=studentCourses.stream().map(StudentCourse::getStudentId).collect(Collectors.toList());//获取所有学生id
            if (studentIds.size()<=0)
                return ResultGenerator.errorResult();
            StudentService service=new StudentServiceImpl();
            //查询学生详细信息
            List<Student> students = service.selectStudentList(studentIds);
            if (students==null)
                return ResultGenerator.errorResult();
            return ResultGenerator.successResult(students);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 删除课程成绩--教师，管理
     * @param courseId
     * @param studentId
     * @return
     */
    public Result delStudentScore(Long courseId,Long studentId){
        TeacherService teacherService=new TeacherServiceImpl();
        try {
            int i = teacherService.delStudentScore(courseId, studentId);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }


}
